from flask import Flask, redirect, url_for
from functools import wraps

app = Flask(__name__)

def log_route(route_name):
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            print(f"访问了路由: {route_name}")
            return f(*args, **kwargs)
        return wrapper
    return decorator

@app.route('/')
@log_route('home')
def home():
    def response():
        return 'Hello, vscode! - 这是首页'
    return response()

@app.route('/user/<username>')
@log_route('show_user')
def show_user(username):
    def response():
        return f'Hello, {username}! - 你访问的是用户页面'
    return response()

@app.route('/submit', methods=['POST'])
@log_route('submit')
def submit():
    def response():
        return 'Form Submitted - 提交成功'
    return response()

@app.route('/post/<int:post_id>')
@log_route('show_post')
def show_post(post_id):
    def response():
        return f'Post {post_id} - 你访问的是 ID 为 {post_id} 的页面'
    return response()

@app.route('/old-page')
@log_route('old_page')
def old_page():
    def redirect_to_new():
        return redirect('/new-page')
    return redirect_to_new()

@app.route('/new-page')
@log_route('new_page')
def new_page():
    def response():
        return 'This is the new page - 这是新的页面'
    return response()

@app.route('/link')
@log_route('link')
def link():
    def response():
        return f'Click to visit user: {url_for("show_user", username="John")}, URL 已自动生成'
    return response()

if __name__ == '__main__':
    app.run(debug=True)
